﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities.Repository;
using VirtualPress.Infrastructure.Entities.Enum;
using System.Linq.Expressions;

namespace VirtualPress.Infrastructure.Entities.Entity
{
    public class Appendix : IAccessible<T_Appendix, Appendix>, ILogicEntity<T_Appendix, Appendix>
    {
        #region Fileds
        public int Id { get; set; }
        public int RaletionId { get; set; }
        public string Own { get; set; }
        public string FileName { get; set; }
        public string AppendixCode { get; set; }
        public AttachmentType AttachmentType { get; set; }
        public string Path { get; set; }
        public string Description { get; set; }
        public DateTime CreateTime { get; set; }
        public AttachmentStatus AttachmentStatus { get; set; }
        /// <summary>
        /// 审稿意见
        /// </summary>
        public string Suggestion { get; set; }
        /// <summary>
        /// 稿件格式
        /// </summary>
        public string Format { get; set; }
        /// <summary>
        /// 稿件大小
        /// </summary>
        public string Size { get; set; }
        #endregion

        protected override void Covariant(T_Appendix model)
        {
            if (model != null)
            {
                this.Id = model.Id;
                this.RaletionId = model.RaletionId;
                this.FileName = model.FileName;
                this.AttachmentType = (Enum.AttachmentType)model.Type;
                this.Path = model.Path;
                this.Description = model.Description;
                this.CreateTime = model.CreateTime;
                this.Own = model.Own;
                this.AttachmentStatus = (Enum.AttachmentStatus)model.Status;
                this.Suggestion = model.Suggestion;
                this.Size = model.Size;
                this.Format = model.Format;
                this.AppendixCode = model.AppendixCode;
            }
        }
        protected override T_Appendix Inverter(Appendix model)
        {
            if (model != null)
            {
                return new T_Appendix()
                {
                    RaletionId=model.RaletionId,
                    CreateTime = model.CreateTime,
                    Description = model.Description,
                    FileName = model.FileName,
                    AppendixCode=model.AppendixCode,
                    Id = model.Id,
                    Path = model.Path,
                    Type = (int)model.AttachmentType,
                    Own=model.Own,
                    Status=(int)model.AttachmentStatus,
                    Suggestion=model.Suggestion,
                    Format=model.Format,
                    Size=model.Size
                };
            }
            return null;
        }
        public Appendix FindLast(Expression<Func<T_Appendix, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.Appendix.Where(where).OrderByDescending(c => c.Id).FirstOrDefault();
                if (item != null)
                {
                    Covariant(item);
                    return this;
                }
                return null;
            }
        }
        public Appendix Set(T_Appendix dbModel)
        {
            Covariant(dbModel);
            return this;
        }
    }
}
